import pygame
import mynewobject,msg,player
import sys,random

pygame.init()

STARNUM = 80
MAXOBJ = 90
GUNFIRETOP = 130
TOTALFIRETOP = 140

def mkenemy(objlist, playerlist):
    o = objlist[random.randint(STARNUM,MAXOBJ)]
    if (o.type == o.NON_TYPE):
        o.set_type(o.ENEMY, playerlist[random.randint(0,1)])
        if (random.randint(1,2) == 1):
            o.smart = 1
            if (random.randint(1,2) == 1):
                o.smart = 2


def rungame(screen, command):

    screensize = screen.get_size()
    screen_width = screensize[0]
    screen_height = screensize[1]

    objectlist = []
    for i in range(TOTALFIRETOP):
        objectlist.append(mynewobject.obj(screen_width, screen_height, objectlist))
        if (i < STARNUM):
            objectlist[i].set_type(objectlist[i].STAR)

#    screen = pygame.display.set_mode((screen_width,screen_height),pygame.DOUBLEBUF)

    if (command == "1P"):
        player1 = player.cplayer(1,screen,screen_width,screen_height,objectlist) #new player1 obj
        player2 = player.cplayer(2,screen,screen_width,screen_height,objectlist) #new player1 obj
        player2.active = False
#        player2.alive = False
    elif (command == "2P"):
        player1 = player.cplayer(1,screen,screen_width,screen_height,objectlist,False) #new player1 obj
        player2 = player.cplayer(2,screen,screen_width,screen_height,objectlist,False) #new player1 obj

    players = [player1,player2]

    clock = pygame.time.Clock()

    

    while((player1.alive and player1.active) or (player2.alive and player2.active)):

        clock.tick(15.7)

        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                sys.exit()
            if event.type == pygame.KEYDOWN:
                if (event.key == pygame.K_s): #player1
                    player1.set_move_down()
                elif (event.key == pygame.K_w): #player1
                    player1.set_move_up()
                elif (event.key == pygame.K_a): #player1
                    player1.set_move_left()
                elif (event.key == pygame.K_d): #player1
                    player1.set_move_right()
                elif (event.key == pygame.K_LCTRL): #player1
                    player1.set_move_fire()
                elif (event.key == pygame.K_DOWN): #player2
                    player2.set_move_down()
                elif (event.key == pygame.K_UP): #player2
                    player2.set_move_up()
                elif (event.key == pygame.K_LEFT): #player2
                    player2.set_move_left()
                elif (event.key == pygame.K_RIGHT): #player2
                    player2.set_move_right()
                elif (event.key == pygame.K_RCTRL): #player2
                    player2.set_move_fire()
                elif (event.key == pygame.K_ESCAPE): #quit game
                    sys.exit();
                else:
                    pass
            if event.type == pygame.KEYUP:
                if (event.key == pygame.K_s): #player1
                    player1.clr_move_down()
                elif (event.key == pygame.K_w): #player1
                    player1.clr_move_up()
                elif (event.key == pygame.K_a): #player1
                    player1.clr_move_left()
                elif (event.key == pygame.K_d): #player1
                    player1.clr_move_right()
                elif (event.key == pygame.K_LCTRL): #player1
                    player1.clr_move_fire()
                elif (event.key == pygame.K_DOWN): #player2
                    player2.clr_move_down()
                elif (event.key == pygame.K_UP): #player2
                    player2.clr_move_up()
                elif (event.key == pygame.K_LEFT): #player2
                    player2.clr_move_left()
                elif (event.key == pygame.K_RIGHT): #player2
                    player2.clr_move_right()
                elif (event.key == pygame.K_RCTRL): #player2
                    player2.clr_move_fire()
                else:
                    pass

        screen.fill((0,0,0))

        for item in players:
#            if (item.alive):
            item.motion()

        for o in objectlist:
            o.update(players) #switch to playerlist
            o.show(screen)
            
        for item in players:
            item.show()
            item.updatescore()

        pygame.display.flip()

        mkenemy(objectlist,players)
        
#        pygame.time.delay(60)

#        print "score: ", player1cnt.score

    

    
